//
//  UIImage+AP.swift
//  APExtensions
//
//  Created by jianbo.pei on 2024/1/4.
//

/**
 ## 参考链接
 - [SwifterSwift - UIImageExtensions.swift](https://github.com/SwifterSwift/SwifterSwift/blob/master/Sources/SwifterSwift/UIKit/UIImageExtensions.swift)
 */

import Foundation

extension UIImage {

    /// 根据颜色和大小生成一张图片
    /// - Parameters:
    ///   - c: 图片的颜色
    ///   - s: 图片的大小
    convenience init(color c: UIColor, size s: CGSize = CGSize(width: 1, height: 1)) {
        let format = UIGraphicsImageRendererFormat()
        format.scale = 1
        guard let img = UIGraphicsImageRenderer(size: s, format: format).image(actions: { context in
            c.setFill()
            context.fill(context.format.bounds)
        }).cgImage else {
            self.init()
            return
        }
        self.init(cgImage: img)
    }
    
}


extension UIImage: APCompatible {}
public extension APBase where Base: UIImage {
    
    /// 根据颜色和大小生成一张图片
    /// - Parameters:
    ///   - c: 图片的颜色
    ///   - s: 图片的大小
    /// - Returns: 生成的图片
    static func image(withColor c: UIColor, size s: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
        return UIImage(color: c, size: s)
    }
    
}
